package com.dotstone.chipism.util;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.Room;
import com.dotstone.chipism.bean.Scene;
import com.dotstone.chipism.bean.ShareInfo;

public class ShareManager {
	private List<Scene> mScenes = new ArrayList<Scene>();
	private List<Room> mRooms = new ArrayList<Room>();
	private List<Device> mDevices = new ArrayList<Device>();
	private static List<ShareInfo> mShares = new ArrayList<ShareInfo>();
	private List<String> mShareSceneIds = new ArrayList<String>();
	private List<String> mShareDeviceIds = new ArrayList<String>();
	private boolean isExists;
	private int shareType;
	public static ShareManager mShareManager = null;

	public static ShareManager getInstance() {
		if (mShareManager == null) {
			mShareManager = new ShareManager();// 单例对象实例
		}
		return mShareManager;
	}

	private ShareManager() {

	}

	public int getShareType() {
		return shareType;
	}

	public void setShareType(int shareType) {
		this.shareType = shareType;
	}

	private Object readResolve() {
		return getInstance();
	}

	public List<Scene> getmScenes() {
		return mScenes;
	}

	public void setmScenes(List<Scene> mScenes) {
		this.mScenes = mScenes;
	}

	public List<Room> getmRooms() {
		return mRooms;
	}

	public void setmRooms(List<Room> mRooms) {
		this.mRooms = mRooms;
	}

	public List<Device> getmDevices() {
		return mDevices;
	}

	public void setmDevices(List<Device> mDevices) {
		this.mDevices = mDevices;
	}

	public static List<ShareInfo> getAllShares() {
		return mShares;
	}

	public static List<ShareInfo> getFamillyShares() {
		List<ShareInfo> familys = new ArrayList<ShareInfo>();
		for (int i = 0; i < mShares.size(); i++) {
			if ((mShares.get(i).getUserId().equals(SocketManager.getInstance().user_id))) {
				familys.add(mShares.get(i));
			}
		}
		return familys;
	}

	public static List<ShareInfo> getFriendShares() {
		List<ShareInfo> friends = new ArrayList<ShareInfo>();
		for (int i = 0; i < mShares.size(); i++) {
			if ((mShares.get(i).getType() == 1)) {
				friends.add(mShares.get(i));
			}
		}
		return friends;
	}

	public static List<Scene> getFamilyScenes() {
		List<Scene> scenes = new ArrayList<Scene>();
		for (int i = 0; i < DeviceManager.getInstance().getScenes().size(); i++) {
			if (DeviceManager.getInstance().getScenes().get(i).getUserId()
					.equals(SocketManager.getInstance().user_id)) {
				scenes.add(DeviceManager.getInstance().getScenes().get(i));
			}
		}
		return scenes;
	}
	
	public static List<Device> getFamillyDevices(){
		List<Device> devices = new ArrayList<Device>();
		List<Device> mAllDevices = DeviceManager.getInstance().getNoWlansDevice();
		for( int i = 0;i<mAllDevices.size();i++){
			Log.i("wmy", "110 "+mAllDevices.get(i).getUserId()+" "+SocketManager.getInstance().user_id);
			if(mAllDevices.get(i).getUserId().equals(SocketManager.getInstance().user_id)){
				devices.add(mAllDevices.get(i));
			}
		}
		return devices;
	}

	public static List<Device> getFriendDevices() {
		List<Device> devices = new ArrayList<Device>();
		List<Device> mAllDevices = DeviceManager.getInstance().getNoLockDevice();
		// 获取朋友分享
		List<ShareInfo> shares = new ArrayList<ShareInfo>();
		String deviceIdStrs = "";
		String[] deviceIds;
		for(int i = 0; i < mShares.size(); i++){
			if(mShares.get(i).getType()==2){
				shares.add(mShares.get(i));
			}
		}
		Log.i("wmy", "朋友分享shares.size() = "+shares.size());
		for (int i = 0; i < shares.size(); i++) {
			if (shares.get(i).getDeviceIds() != null && (!shares.get(i).getDeviceIds().equals(""))) {
				deviceIdStrs += (shares.get(i).getDeviceIds() + ",");
			}
		}
		// 截取掉最后面的逗号
		if (deviceIdStrs.endsWith(",")) {
			deviceIdStrs = deviceIdStrs.substring(0, deviceIdStrs.length() - 1);
		}
		Log.i("wmy", "朋友分享deviceIdStrs = "+deviceIdStrs);
		// 通过逗号截取掉出字符串数组
		deviceIds = deviceIdStrs.split(",");
		for (int i = 0; i < mAllDevices.size(); i++) {
			boolean e = false;
			Log.i("wmy", "mAllDevices.get(i).getDeviceName() = "+mAllDevices.get(i).getDeviceName());
			for (String id : deviceIds) {
				if (id.equals(mAllDevices.get(i).getDeviceID())) {
					Log.i("wmy", id+" = "+mAllDevices.get(i).getDeviceID());
					e = true;
				}
			}
			if (!e) {
				devices.add(mAllDevices.get(i));
			}
		}
		return devices;
	}

	public static List<Scene> getFriendScenes() {
		List<Scene> scenes = new ArrayList<Scene>();
		List<Scene> mAllScene = DeviceManager.getInstance().getScenes();
		// 获取朋友分享
		List<ShareInfo> shares = new ArrayList<ShareInfo>();
		String sceneIdStrs = "";
		String[] sceneIds;
		// 遍历分享 获取除了朋友之外的分享
		for(int i = 0; i < mShares.size(); i++){
			if(mShares.get(i).getType()==2){
				shares.add(mShares.get(i));
			}
		}
		for (int i = 0; i < shares.size(); i++) {
			if (shares.get(i).getSceneIds() != null && (!shares.get(i).getSceneIds().equals(""))) {
				sceneIdStrs += (shares.get(i).getSceneIds() + ",");
			}
		}
		Log.i("wmy", "获取情景ids"+sceneIdStrs);
		// 截取掉最后面的逗号
		if (sceneIdStrs.endsWith(",")) {
			sceneIdStrs = sceneIdStrs.substring(0, sceneIdStrs.length() - 1);
		}
		// 通过逗号截取掉出字符串数组
		sceneIds = sceneIdStrs.split(",");
		for (int i = 0; i < mAllScene.size(); i++) {
			boolean e = false;
			for (String id : sceneIds) {
				if (id.equals(mAllScene.get(i).getSceneId())) {
					e = true;
				}
			}
			if (!e) {
				scenes.add(mAllScene.get(i));
			}
		}
		return scenes;
	}

	public List<ShareInfo> getmShares() {
		List<ShareInfo> ss = new ArrayList<ShareInfo>();
		Log.i("wmy", "shareType = " + shareType);
		switch (shareType) {
		case 1:
			for (int i = 0; i < mShares.size(); i++) {
				if (mShares.get(i).getType() == shareType) {
					ss.add(mShares.get(i));
					Log.i("wmy", "添加分享" + mShares.get(i).getShareName() + " " + mShares.get(i).getType());
				}
			}
			break;
		case 2:
			for (int i = 0; i < mShares.size(); i++) {
				if (mShares.get(i).getType() == shareType) {
					ss.add(mShares.get(i));
					Log.i("wmy", "添加分享" + mShares.get(i).getShareName() + " " + mShares.get(i).getType());
				}
			}
			break;
		case 3:
			for (int i = 0; i < mShares.size(); i++) {
				if (mShares.get(i).getType() == shareType) {
					ss.add(mShares.get(i));
					Log.i("wmy", "添加分享" + mShares.get(i).getShareName() + " " + mShares.get(i).getType());
				}
			}
			break;
		case 4:
			for (int i = 0; i < mShares.size(); i++) {
				if (mShares.get(i).getType() == shareType) {
					ss.add(mShares.get(i));
					Log.i("wmy", "添加分享" + mShares.get(i).getShareName() + " " + mShares.get(i).getType());
				}
			}
			break;

		default:
			break;
		}
		return ss;
	}

	public void setmShares(List<ShareInfo> mShares) {
		this.mShares = mShares;
	}

	public void addShare(ShareInfo share) {
		isExists = false;
		for (int i = 0; i < mShares.size(); i++) {

			if (mShares.get(i).getShareInfoId().equals(share.getShareInfoId())) {
				isExists = true;
			}
		}
		if (!isExists) {
			mShares.add(share);
		}
	}

	public void clearShare() {
		mShares.clear();
	}

	public void clear() {
		mDevices.clear();
		mRooms.clear();
		mScenes.clear();
	}

}
